﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MappitLib {
    public partial class LayerBrowserItem : UserControl {
        //*********************************************************************
        #region Members
        private Layer layer_;
        private TilemapEditor tilemap_editor_;
        private bool is_selected_;
        #endregion
        //*********************************************************************
        #region Properties
        public bool LayerVisible {
            get {
                return checkVisible.Checked;
            }
            set {
                checkVisible.Checked = value;
                layer_.Visible = value;
            }
        }
        public string LayerName {
            get {
                return layer_.Name;
            }
            set {
                labelLayerName.Text = value;
                layer_.Name = value;
            }
        }
        public Layer Layer {
            get {
                return layer_;
            }
            set {
                SetLayer(value);
            }
        }
        public bool IsSelected {
            get {
                return is_selected_;
            }
            set {
                is_selected_ = value;
                // set background color
                if (is_selected_) {
                    BackColor = Color.Blue;
                } else {
                    BackColor = SystemColors.Control;
                }
                // set tilemap editor's active layer
                if (tilemap_editor_ != null) {
                    tilemap_editor_.ActiveLayer = Layer;
                }
            }
        }
        #endregion
        //*********************************************************************
        #region Constructors
        public LayerBrowserItem(LayersControl parent, Layer layer) {
            InitializeComponent();
            Init_(parent, layer);
        }
        private void Init_(LayersControl parent, Layer layer) {
            IsSelected = false;
            Parent = parent.itemsPanel;
            tilemap_editor_ = parent.TilemapEditor;
            SetLayer(layer);
        }
        #endregion
        //*********************************************************************
        #region Methods
        public void SetLayer(Layer value) {
            layer_ = value;
            if (layer_ == null) {
                checkVisible.Checked = false;
                labelLayerName.Text = "";
                Visible = false;
            } else {
                checkVisible.Checked = layer_.Visible;
                labelLayerName.Text = layer_.Name;
                Visible = true;
                layer_.Tilemap.TileSet = tilemap_editor_.Tileset;
            }
        }
        private void DestroyLabelTextbox(TextBox textbox) {
            textbox.Dispose();
            labelLayerName.Show();
        }
        public void RenameLayer() {
            TextBox textbox = CreateLayerTextbox();
        }

        private TextBox CreateLayerTextbox() {
            // create edit control above label
            TextBox textbox = new TextBox();
            textbox.Parent = this;
            textbox.Location = labelLayerName.Location;
            textbox.Text = labelLayerName.Text;
            textbox.BorderStyle = BorderStyle.FixedSingle;
            textbox.Select();
            textbox.Show();
            textbox.LostFocus += new EventHandler(textbox_LostFocus);
            textbox.PreviewKeyDown += new PreviewKeyDownEventHandler(textbox_PreviewKeyDown);
            labelLayerName.Hide();
            return textbox;
        }
        #endregion
        //*********************************************************************
        #region Event handlers
        private void LayerBrowserItem_Click(object sender, EventArgs e) {
            tilemap_editor_.ActiveLayer = this.layer_;
        }

        private void labelLayerName_DoubleClick(object sender, EventArgs e) {
            RenameLayer();
        }

        void textbox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
            if (e.KeyCode == Keys.Enter) {
                this.Focus();
            }
        }

        void textbox_LostFocus(object sender, EventArgs e) {
            LayerName = ((TextBox)sender).Text;
            DestroyLabelTextbox((TextBox)sender);
        }

        private void checkVisible_CheckedChanged(object sender, EventArgs e) {
            layer_.Visible = checkVisible.Checked;
            tilemap_editor_.Refresh();
        }
    }
        #endregion
}
